﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace LocalCronjob
{
	public partial class MainForm : Form
	{
		IDictionary<string, cronjob> _cronjobs = new Dictionary<string, cronjob>();
		private int _seconds;
		DateTime _myDate;
		
		public MainForm()
		{
			InitializeComponent();
			_myDate = DateTime.Now;
			_seconds = _myDate.Second;
			InitCronjobs();
		}
		
		private void InitCronjobs()
		{
			_cronjobs.Clear();
			lb_current.Items.Clear();
			log.Clear();
			
			string[] files = Directory.GetFiles(Application.StartupPath);
			DateTime now = DateTime.Now;
			
			foreach(string f in files)
			{
				int last_sp = f.LastIndexOf('\\')+1;
				string file = f.Substring(last_sp);
				
				string[] cj_file = file.Split('.');
								
				if(cj_file[1] == "cj")
				{
					addLog(file + " loaded");
					string line = "";
					string name = "";
					string link = "";
					ArrayList times = new ArrayList();
					int n = 0;
					StreamReader sr = new StreamReader(file);
					while(!sr.EndOfStream)
					{
						line = sr.ReadLine();
						switch(n)
						{
							case 0:
								name = line;
								break;
							case 1:
								link = line;
								break;
							default:
								string[] t = line.Split(',');
								if(t.Length == 2)
								{
									DateTime dt = new DateTime(now.Year, now.Month, now.Day, int.Parse(t[0]), int.Parse(t[1]), 0);
									times.Add(dt);
								}
								break;
						}
						n++;
					}
					sr.Close();
					
					if(!_cronjobs.ContainsKey(name))
					{
						cronjob cj = new cronjob(name, link, times, this);
						_cronjobs.Add(cj.Name, cj);
						lb_current.Items.Add(cj.Name);
					}
				}
			}
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			
		}
		
		void GroupBox2Enter(object sender, EventArgs e)
		{
			
		}
		
		void Label4Click(object sender, EventArgs e)
		{
			
		}
		
		void Timer1Tick(object sender, EventArgs e)
		{
			_seconds++;
			if(_seconds == 60)
			{				
				_myDate = DateTime.Now;
				_seconds = _myDate.Second;
				CheckForExecution();
			}
			
			if(_seconds%2 == 0)
				l_time.Text = _myDate.Hour.ToString()+":"+_myDate.Minute.ToString();
			else
				l_time.Text = _myDate.Hour.ToString()+"."+_myDate.Minute.ToString();
		}
		
		private void CheckForExecution()
		{
			foreach(cronjob cj in _cronjobs.Values)
			{
				foreach(DateTime dt in cj.Times)
				{
					if(dt.Hour == _myDate.Hour && dt.Minute == _myDate.Minute)
					{
						cj.Execute();
					}
				}
			}
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			ArrayList times = new ArrayList();
			DateTime now = DateTime.Now;
			
			foreach(string hr in lb_new_hr.CheckedItems)
			{
				foreach(string min in lb_new_min.CheckedItems)
				{					
					DateTime dt = new DateTime(now.Year, now.Month, now.Day, int.Parse(hr), int.Parse(min), 0);
					times.Add(dt);
				}
			}				
			
			string new_name = tb_new_name.Text;
			if(_cronjobs.ContainsKey(tb_new_name.Text))
			{
				int n = 0;				
				do
				{
					n++;
					new_name = tb_new_name.Text + "_" + n.ToString();
				}while(_cronjobs.ContainsKey(new_name));
			}
			
			
			cronjob cj = new cronjob(new_name, tb_new_link.Text, times, this);
			_cronjobs.Add(cj.Name, cj);
			
			// add to list
			lb_current.Items.Add(cj.Name);
			
			addLog("New Cronjob \"" + cj.Name + "\" (" + times.Count.ToString() + "x a day)");
			
			tb_new_name.Text = "";
			tb_new_link.Text = "http://localhost/";
			cb_new_presets.Text = "";
			lb_new_hr.ClearSelected();
			lb_new_min.ClearSelected();
			
			cj.SaveCronjob();
		}
		
		public void addLog(string text)
		{
			log.Text = text + "\n" + log.Text;
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			InitCronjobs();
		}
		
		void Lb_currentSelectedIndexChanged(object sender, EventArgs e)
		{
			cronjob cj;
			l_hours.Clear();
			l_mins.Clear();
			cj = _cronjobs[lb_current.SelectedItem.ToString()];
			IDictionary<int, int> hs = new Dictionary<int, int>();
			IDictionary<int, int> mins = new Dictionary<int, int>();
			
			foreach(DateTime dt in cj.Times)
			{
				if(!hs.Keys.Contains(dt.Hour))
					hs.Add(dt.Hour, dt.Hour);
				if(!mins.Keys.Contains(dt.Minute))
					mins.Add(dt.Minute, dt.Minute);
			}
			
			foreach(int h in hs.Values)
			{
				l_hours.AppendText(" " + h.ToString());
			}
			
			foreach(int m in mins.Values)
			{
				l_mins.AppendText(" " + m.ToString());
			}
			
			l_link.Text = cj.Link;
			if(cj.LastCall.Year != 2000)
				l_called.Text = cj.LastCall.Day.ToString()+"."+cj.LastCall.Month.ToString()+"."+cj.LastCall.Year.ToString()+" "+cj.LastCall.Hour.ToString()+":"+cj.LastCall.Minute.ToString();
			else
				l_called.Text = "never";
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			cronjob cj;
			cj = _cronjobs[lb_current.SelectedItem.ToString()];
			File.Delete(cj.Name + ".cj");
			InitCronjobs();
		}
		
		public void navTo(string path)
		{
			webBrowser1.Navigate(path);
		}
		
		void NotifyIcon1MouseDoubleClick(object sender, MouseEventArgs e)
		{
			
		}
		
		void ToolStripMenuItem1Click(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Normal;
		}
		
		void ToolStripMenuItem2Click(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void Button4Click(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Minimized;
		}
	}
}
